/***************************************************************************
 *   Copyright (C) 2008 by						   *
 *          Ali Abdou							   *
 *          Camille Gallet						   *
 *          R�my Hubscher						   *
 *          Regis Strub							   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <QtGui>
#include "GameFrame.hpp"

GameFrame::GameFrame(QWidget * parent)
    :QMainWindow(parent)
{
    board = Board::getInstance();
    setupUi(this);

    setCentralWidget(board);
    resize(300,300);

    // Les actions
    createActions();
}

void GameFrame::createActions()
{
    /*** JEU ***/
    // Nouvelle partie
    connect(actionNouvellePartie, SIGNAL(triggered()), this, SLOT(newGame()));
    // Pr�f�rences
    // Menu Quitter
    connect(actionQuitter, SIGNAL(triggered()), this, SLOT(close()));

    /*** AFFICHAGE ***/
    // Coups possibles
    // Coups pr�c�dents
    
    /*** Aide ***/
    // R�gles du Jeu
     connect(actionCoupPrecedent, SIGNAL(triggered()), (QObject*) GameEngine::getInstance(), SLOT(revertCoup()));
     /*connect(actionCoupSuivant, SIGNAL(triggered()), (QObject*) GameEngine::getInstance(), SLOT(nextCoup()));*/
   // A propos de Force3
    connect(actionAboutForce3, SIGNAL(triggered()), this, SLOT(aboutForce3()));
    // A propos de Qt
    connect(actionAboutQt, SIGNAL(triggered()), qApp , SLOT(aboutQt()));
    connect(actionPreferences, SIGNAL(triggered()), (QObject*) this , SLOT(getConfig()));
    connect((QObject*)this, SIGNAL(writeConf(QString, QString, bool, bool)), (QObject*) GameEngine::getInstance() , SLOT(changeConf(QString, QString, bool, bool)));
}

void GameFrame::aboutRules(){


}

void GameFrame::getConfig(){
    bool h1,h2;
    dlg = new QDialog();
    nP1 = new QLineEdit("",dlg);
    nP2 = new QLineEdit("",dlg);
    humanP1 = new QCheckBox(dlg);
    humanP2 = new QCheckBox(dlg);
    QLabel lnP1("Nom du joueur 1",dlg),lnP2("Nom du joueur 2",dlg),lhP1("Joueur 1 Humain",dlg),lhP2("Joueur 2 Humain",dlg);
    QPushButton btnOk("OK",dlg),btnAnnuler("Annuler",dlg);
    QGridLayout *layout = new QGridLayout;
    connect((QObject*)&btnOk,SIGNAL(clicked()),(QObject*) this, SLOT(confOk()));
    connect((QObject*)&btnAnnuler,SIGNAL(clicked()),(QObject*) this, SLOT(confAnnuler()));
    QSettings settings("UTBM","force3");
    nP1->setText(settings.value("p1Name", "Bob's Crosses").toString());
    nP2->setText(settings.value("p2Name","Alice's Noughts").toString());
    h1=settings.value("p1Human",false).toBool();
    h2=settings.value("p2Human",true).toBool();
    humanP1->setCheckState(h1?Qt::Checked:Qt::Unchecked);
    humanP1->setCheckState(h2?Qt::Checked:Qt::Unchecked);
     layout->addWidget(&lnP1,0,0,Qt::AlignLeft);
     layout->addWidget(nP1,0,1,Qt::AlignLeft);
     layout->addWidget(&lnP2,1,0,Qt::AlignLeft);
     layout->addWidget(nP2,1,1,Qt::AlignLeft);
     layout->addWidget(&lhP1,2,0,Qt::AlignLeft);
     layout->addWidget(humanP1,2,1,Qt::AlignLeft);
     layout->addWidget(&lhP2,3,0,Qt::AlignLeft);
     layout->addWidget(humanP2,3,1,Qt::AlignLeft);
     layout->addWidget(&btnOk,4,0,Qt::AlignLeft);
     layout->addWidget(&btnAnnuler,4,1,Qt::AlignRight);
      dlg->setLayout(layout);
    dlg->exec();
}

void GameFrame::confOk(){
    dlg->close();
    emit writeConf(nP1->text(), nP2->text(), (humanP1->checkState()==Qt::Checked), (humanP2->checkState()==Qt::Checked));
    if(nP1!=NULL)  delete nP1;
    if(nP2!=NULL) delete nP2;
    if(humanP1!=NULL) delete humanP1;
    if(humanP2!=NULL) delete humanP2;
}

void GameFrame::confAnnuler(){
    dlg->close();
    if(nP1!=NULL)  delete nP1;
    if(nP2!=NULL) delete nP2;
    if(humanP1!=NULL) delete humanP1;
    if(humanP2!=NULL) delete humanP2;
}

void GameFrame::aboutForce3()
{
    QMessageBox::information(this, tr("� propos de Force 3"), tr("Force3 est un application libre sous licence GPL v2. R�alis�e dans le cadre d'un projet � l'UTBM"));
}

void GameFrame::recupBoard(int * b)
{
   board->setBoard((GameEngine::box *)b);
}

void GameFrame::connecting(GameEngine* ge)
{
    connect(ge, SIGNAL(newBoard(int *)), this, SLOT(recupBoard(int *)));
}

void GameFrame::newGame(){
	/*GameEngine::box board[9]={GameEngine::SQUARE_ONLY,	GameEngine::SQUARE_ONLY,	GameEngine::SQUARE_ONLY,
				      GameEngine::SQUARE_ONLY,	GameEngine::EMPTY,		GameEngine::SQUARE_ONLY,
				      GameEngine::SQUARE_ONLY,	GameEngine::SQUARE_ONLY,	GameEngine::SQUARE_ONLY};
	this->board->setBoard(board);
	this->board->setConnect(true);
	GameEngine::getInstance()->jouer();*/
	GameEngine::getInstance()->newGame();
}

void GameFrame::recupAskingMove(GameEngine::player){
    Moving m;
     emit getMove(m);
}
